package com.sphd.education.activitys;

import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.sphd.education.R;
import com.sphd.education.base.BaseActivity;
import com.sphd.education.info.MyInfo;
import com.sphd.education.info.PushInfo;
import com.sphd.education.info.UpdataInfo;
import com.sphd.education.modle.EducationUrl;
import com.sphd.education.utils.CheckUpdate;
import com.sphd.education.utils.UpdataInfoParser;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* 设置界面
* */
public class SettingActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.acceptmessage_switch)
    Switch acceptmessageSwitch;
    @Bind(R.id.voice_switch)
    Switch voiceSwitch;
    @Bind(R.id.shock_switch)
    Switch shockSwitch;
    @Bind(R.id.disturbance_switch)
    Switch disturbanceSwitch;
    @Bind(R.id.start_time)
    TextView startTime;
    @Bind(R.id.end_time)
    TextView endTime;
    @Bind(R.id.zuxiao)
    Button zuxiao;
    @Bind(R.id.start_time_card_view)
    CardView startTimeCardView;
    @Bind(R.id.end_time_card_view)
    CardView endTimeCardView;
    private Intent intent;
    private Context context;
    private PushInfo pushInfo;//我的实体类存储设置的相关信息
    private MyInfo myInfo;
    private int start_Hour;
    private int end_hour;
    private int start_Min;
    private int end_Min;

    //定义显示时间控件
    private Calendar calendar; //通过Calendar获取系统时间
    private int mHour;
    private int mMinute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//这是自带的箭头的返回的监听
            }
        });
        initView();

    }

    @Override
    public void initView() {
        intent = new Intent();
        context = SettingActivity.this;
        calendar = Calendar.getInstance();
        pushInfo = DataSupport.findLast(PushInfo.class);
        myInfo = DataSupport.findLast(MyInfo.class);
        if (pushInfo == null) {
            Toast.makeText(context, "尚未登录前去登陆界面", Toast.LENGTH_SHORT).show();
            pushInfo = new PushInfo();
            pushInfo.setAcceptmessage(acceptmessageSwitch.isChecked());
            pushInfo.setVoice(voiceSwitch.isChecked());
            pushInfo.setShock(shockSwitch.isChecked());
            pushInfo.setDisturbance(disturbanceSwitch.isChecked());
            pushInfo.setStarttime(startTime.getText().toString().trim());
            pushInfo.setEndtime(endTime.getText().toString().trim());
            pushInfo.save();
        } else {
            acceptmessageSwitch.setChecked(pushInfo.isAcceptmessage());
            voiceSwitch.setChecked(pushInfo.isVoice());
            shockSwitch.setChecked(pushInfo.isShock());
            disturbanceSwitch.setChecked(pushInfo.isDisturbance());
            if (!TextUtils.isEmpty(pushInfo.getStarttime())) {
                startTime.setText(pushInfo.getStarttime());
            }
            if (TextUtils.isEmpty(pushInfo.getEndtime())) {
                endTime.setText(pushInfo.getEndtime());
            }
            if(pushInfo.isDisturbance()){
                startTimeCardView.setVisibility(View.VISIBLE);
                endTimeCardView.setVisibility(View.VISIBLE);
            }else{
                startTimeCardView.setVisibility(View.GONE);
                endTimeCardView.setVisibility(View.GONE);
            }
        }
        findViewById();
    }

    @Override
    public void findViewById() {
        myOnClick();
    }

    @Override
    public void myNet() {

    }

    private void myOnClick() {
        MyOnCheckedChangeListener myOnCheckedChangeListener = new MyOnCheckedChangeListener();
        acceptmessageSwitch.setOnCheckedChangeListener(myOnCheckedChangeListener);
        voiceSwitch.setOnCheckedChangeListener(myOnCheckedChangeListener);
        shockSwitch.setOnCheckedChangeListener(myOnCheckedChangeListener);
        disturbanceSwitch.setOnCheckedChangeListener(myOnCheckedChangeListener);

    }

    @OnClick(R.id.zuxiao)
    public void setZuxiaoOnClick() {
        if (myInfo == null) {
            EducationUrl.showToast(context, "尚未登录");
        } else {
            if (myInfo.getLogon_status() == 0) {
                EducationUrl.showToast(context, "尚未登录");
            } else {
                DataSupport.deleteAll(MyInfo.class);
                EducationUrl.showToast(context, "已清空所有登陆信息");
            }
        }

    }

    class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int buttonViewId = buttonView.getId();

            switch (buttonViewId) {
                case R.id.acceptmessage_switch:
                    Toast.makeText(context, "监听接受", Toast.LENGTH_SHORT).show();
                    changeSwitch("acceptmessage", isChecked);
                    if (isChecked) {
                        PushManager.startWork(getApplicationContext(),
                                PushConstants.LOGIN_TYPE_API_KEY, "xR5le9ebbVmdRj9pCXSsVGDX");

                        Toast.makeText(context, "启动", Toast.LENGTH_SHORT).show();
                    } else {
                        PushManager.stopWork(context);
                        Toast.makeText(context, "关闭", Toast.LENGTH_SHORT).show();
                    }

                    break;
                case R.id.voice_switch:
                    Toast.makeText(context, "监听声音", Toast.LENGTH_SHORT).show();
                    changeSwitch("voice", isChecked);
                    break;
                case R.id.shock_switch:
                    Toast.makeText(context, "监听震动", Toast.LENGTH_SHORT).show();
                    changeSwitch("shock", isChecked);
                    break;
                case R.id.disturbance_switch:
                    Toast.makeText(context, "监听勿扰", Toast.LENGTH_SHORT).show();
                    changeSwitch("disturbance", isChecked);
                    if(isChecked){
                        startTimeCardView.setVisibility(View.VISIBLE);
                        endTimeCardView.setVisibility(View.VISIBLE);
                    }else {
                        startTimeCardView.setVisibility(View.GONE);
                        endTimeCardView.setVisibility(View.GONE);
                        startTime.setText("00:00");
                        endTime.setText("00:00");
                        ContentValues values = new ContentValues();
                        values.put("starttime", startTime.getText().toString().trim());
                        values.put("endtime", endTime.getText().toString().trim());
                        DataSupport.update(PushInfo.class, values, pushInfo.getId());
                        PushManager.setNoDisturbMode(context, 0, 0, 0, 0);
                    }
                    break;
                default:
                    break;


            }
        }
    }

    private void changeSwitch(String key, Boolean isChecked) {

        ContentValues values = new ContentValues();
        values.put(key, isChecked);
        DataSupport.update(PushInfo.class, values, pushInfo.getId());

    }

    @OnClick(R.id.start_time)
    public void setStartTimeOnClick() {
        new TimePickerDialog(context,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hour, int minute) {
                        // TODO Auto-generated method stub
                        mHour = hour;
                        mMinute = minute;
                        start_Hour =hour;
                        start_Min=minute;
                        //更新EditText控件时间 小于10加0
                        startTime.setText(new StringBuilder()
                                .append(mHour < 10 ? "0" + mHour : mHour).append(":")
                                .append(mMinute < 10 ? "0" + mMinute : mMinute));
                        ContentValues values = new ContentValues();
                        values.put("starttime", startTime.getText().toString().trim());
                        DataSupport.update(PushInfo.class, values, pushInfo.getId());
                        PushManager.setNoDisturbMode(context,start_Hour,start_Min,0,0);
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), true).show();
    }

    @OnClick(R.id.end_time)
    public void setEndTimeOnClick() {
        new TimePickerDialog(context,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hour, int minute) {
                        // TODO Auto-generated method stub
                        mHour = hour;
                        mMinute = minute;
                        end_hour=hour;
                        end_Min=minute;
                        //更新EditText控件时间 小于10加0
                        endTime.setText(new StringBuilder()
                                .append(mHour < 10 ? "0" + mHour : mHour).append(":")
                                .append(mMinute < 10 ? "0" + mMinute : mMinute));
                        ContentValues values = new ContentValues();
                        values.put("endtime", endTime.getText().toString().trim());
                        DataSupport.update(PushInfo.class, values, pushInfo.getId());
                        PushManager.setNoDisturbMode(context, start_Hour, start_Min, end_hour, end_Min);
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), true).show();
    }



}
